from django.shortcuts import render, redirect, HttpResponse
from web.forms.project import ProjectModelForm
from django.http import JsonResponse
from web import models


def project_list(request):
    if request.method == 'GET':

        # GET请求查看项目列表
        project_dict = {'star': [], 'my': [], 'join': []}
        my_project_list = models.Project.objects.filter(creator=request.tracer.user).all()
        for row in my_project_list:
            if row.star:
                project_dict['star'].append({"value": row, "type": 'my'})
                # {"value": row, "type": 'my'}
            else:
                project_dict['my'].append(row)
        join_project_list = models.ProjectUser.objects.filter(user=request.tracer.user).all()
        for item in join_project_list:
            if item.star:
                project_dict['star'].append({"value": item.project, "type": 'join'})
                # {"value": item.project, "type": 'join'}
            else:
                project_dict['join'].append(item.project)

        form = ProjectModelForm(request)
        return render(request, 'project_list.html', {'form': form, 'project_dict': project_dict})
    form = ProjectModelForm(request, request.POST)
    if form.is_valid():
        # 验证通过后保存 项目名、颜色、描述、creator创建者
        form.instance.creator = request.tracer.user
        form.save()
        return JsonResponse({'status': True})
    return JsonResponse({'status': False, 'error': form.errors})


def project_star(request, project_type, project_id):
    '''星标项目'''
    if project_type == 'my':
        models.Project.objects.filter(id=project_id, creator=request.tracer.user).update(star=True)
        return redirect('project_list')
    if project_type == 'join':
        models.Project.objects.filter(id=project_id, user=request.tracer.user).update(star=True)
        return redirect('project_list')
    return HttpResponse('请求错误')


def project_unstar(request, project_type, project_id):
    '''取消星标'''
    if project_type == 'my':
        models.Project.objects.filter(id=project_id, creator=request.tracer.user).update(star=False)
        return redirect('project_list')
    if project_type == 'join':
        models.Project.objects.filter(id=project_id, user=request.tracer.user).update(star=False)
        return redirect('project_list')
    return HttpResponse('请求错误')
